package cn.miao.photoprinter.activity;

import java.lang.ref.WeakReference;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import cn.miao.photoprinter.R;
import cn.miao.photoprinter.adapter.ShoppingOrderAdapter;
import cn.miao.photoprinter.database.DatabaseHelper;
import cn.miao.photoprinter.entity.ShoppingCart;

public class ShoppingOrderActivity extends Activity {

	private static final int LOADOK = 10001;
	private static Handler handler;
	ListView shoppingCart;
	Button btRemark;
	ShoppingOrderAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_shoppingorder);
		handler = new MyHandler(this);

		shoppingCart = (ListView) findViewById(R.id.shoppingcart);
//		shoppingCart
//				.setOffsetLeft(getResources().getDisplayMetrics().widthPixels * 2 / 3);
		// dose not work
		shoppingCart.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				ShoppingOrderAdapter adapter = (ShoppingOrderAdapter) parent.getAdapter();
				ShoppingCart cart = adapter.getItem(position);
				System.out.println("ItemLongClick:" + cart.getShop());
				return false;
			}
		});
		shoppingCart.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ShoppingOrderAdapter adapter = (ShoppingOrderAdapter) parent.getAdapter();
				ShoppingCart cart = adapter.getItem(position);
				System.out.println("ItemClick:" + cart.getShop());
			}
		});
		
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					List<ShoppingCart> records = new DatabaseHelper(
							ShoppingOrderActivity.this).queryShoppingCart();
					Message msg = new Message();
					msg.what = LOADOK;
					msg.obj = records;
					handler.sendMessage(msg);
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}).start();
		
		btRemark = (Button) findViewById(R.id.bt_remark);
		btRemark.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			}
		});
	}
	
	@Override
	public void onBackPressed() {
		setResult(Activity.RESULT_OK, new Intent());
		finish();
	}
	
	static class MyHandler extends Handler {

		WeakReference<ShoppingOrderActivity> context;

		public MyHandler(ShoppingOrderActivity context) {
			this.context = new WeakReference<ShoppingOrderActivity>(context);
		}

		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			ShoppingOrderActivity activity = context.get();
			switch (msg.what) {
			case LOADOK:
				List<ShoppingCart> records = (List<ShoppingCart>) msg.obj;
				activity.adapter = new ShoppingOrderAdapter(activity,
						records);
				activity.shoppingCart.setAdapter(activity.adapter);
				break;
			}
			super.handleMessage(msg);
		}
	}
}
